package com.qmyx.supply.admin.controller;

import com.qmyx.supply.admin.annotation.oplog.annotation.OperationLog;
import com.ytjj.qmyx.supplychain.common.api.CommonPage;
import com.ytjj.qmyx.supplychain.common.api.CommonResult;
import com.ytjj.qmyx.supplychain.common.constants.BankerConstants;
import com.ytjj.qmyx.supplychain.common.constants.SysLogConstants;
import com.ytjj.qmyx.supplychain.common.model.request.FreigRequest;
import com.ytjj.qmyx.supplychain.common.model.request.YxAssignFreight;
import com.ytjj.qmyx.supplychain.common.model.request.YxAstrictRegion;
import com.ytjj.qmyx.supplychain.common.model.request.YxFreightTemplate;
import com.ytjj.qmyx.supplychain.mapper.entity.YxBanker;
import com.ytjj.qmyx.supplychain.service.BankerService;
import com.ytjj.qmyx.supplychain.service.Impl.FreightTemplateService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 *
 * 运费模板
 */
@RestController
@AllArgsConstructor
@RequestMapping("/freightTemplate")
public class FreightTemplateController {

    private FreightTemplateService freightTemplateService;

    private BankerService bankerService;

    /**
     * 模板列表
     * @return
     */
    @PostMapping("/page")
    public CommonResult selectFreightTemplatePage(@RequestBody FreigRequest freigRequest) {
        YxBanker userInfo = bankerService.getUserInfo();
        if (!userInfo.getLevel().equals(BankerConstants.LEVEL_PLATFORM)) {
            freigRequest.setBankerId(userInfo.getId());
        }
        CommonPage commonPage = freightTemplateService.selectFreightTemplatePage(freigRequest);
        return CommonResult.success(commonPage);
    }

    /**
     * 新建模板
     * @param yxFreightTemplate
     * @return
     */
    @PostMapping("/insertFreightTemplate")
    @OperationLog(optModule = "模板", optType = SysLogConstants.OPERATION_ADD, desc = "新增运费模板")
    public CommonResult insertFreightTemplate(@RequestBody YxFreightTemplate yxFreightTemplate) {
        Integer bankerId = bankerService.getUserInfo().getId();
        yxFreightTemplate.setBankerId(bankerId);
        return CommonResult.success(freightTemplateService.insertFreightTemplate(yxFreightTemplate));
    }

    /**
     * 模板详情
     * @return
     */
    @GetMapping("/selectYxFreightTemplate")
    public CommonResult selectFreightTemplateResponse(Integer id) {
        return CommonResult.success(freightTemplateService.selectFreightTemplateResponse(id));
    }

    /**
     * 模板删除
     * @return
     */
    @GetMapping("/deleteFreightTemplate")
    @OperationLog(optModule = "模板", optType = SysLogConstants.OPERATION_DELETE, desc = "删除模板")
    public CommonResult deleteFreightTemplate(Integer id) {
        return CommonResult.success(freightTemplateService.deleteFreightTemplate(id));
    }

    /**
     * 删除指定运费
     * @return
     */
    @GetMapping("/deleteAssignFreight")
    @OperationLog(optModule = "模板", optType = SysLogConstants.OPERATION_DELETE, desc = "删除指定运费")
    public CommonResult deleteAssignFreight(Integer id) {
        return CommonResult.success(freightTemplateService.deleteAssignFreight(id));
    }

    /**
     * 删除限制下单区域
     * @return
     */
    @GetMapping("/deleteAstrictRegion")
    @OperationLog(optModule = "模板", optType = SysLogConstants.OPERATION_DELETE, desc = "删除限制下单区域")
    public CommonResult deleteAstrictRegion(Integer id) {
        return CommonResult.success(freightTemplateService.deleteAstrictRegion(id));
    }

    /**
     * 修改指定运费
     * @param yxAssignFreight
     * @return
     */
    @PostMapping("/updateAssignFreight")
    @OperationLog(optModule = "模板", optType = SysLogConstants.OPERATION_UPDATE, desc = "修改指定运费")
    public CommonResult updataAssignFreight(@RequestBody YxAssignFreight yxAssignFreight) {
        return CommonResult.success(freightTemplateService.updataAssignFreight(yxAssignFreight));
    }

    /**
     * 修改限制下单区域
     * @param yxAstrictRegion
     * @return
     */
    @PostMapping("/updateAstrictRegion")
    @OperationLog(optModule = "模板", optType = SysLogConstants.OPERATION_UPDATE, desc = "修改限制下单区域")
    public CommonResult updataAstrictRegion(@RequestBody YxAstrictRegion yxAstrictRegion) {
        return CommonResult.success(freightTemplateService.updataAstrictRegion(yxAstrictRegion));
    }

    /**
     * 修改运费模板
     * @param yxFreightTemplate
     * @return
     */
    @PostMapping("/updateFreightTemplate")
    @OperationLog(optModule = "模板", optType = SysLogConstants.OPERATION_UPDATE, desc = "修改运费模板")
    public CommonResult updataFreightTemplate(@RequestBody YxFreightTemplate yxFreightTemplate) {
        return CommonResult.success(freightTemplateService.updataFreightTemplate(yxFreightTemplate));
    }

    /**
     * 修改运费默认
     * @param id
     * @return
     */
    @GetMapping("/updateDefaults")
    @OperationLog(optModule = "模板", optType = SysLogConstants.OPERATION_UPDATE, desc = "修改运费默认")
    public CommonResult updateDfaults(Integer id) {
        return CommonResult.success(freightTemplateService.updateDfaults(id));
    }
}
